class Solution {
    public:
        int is_existent[300][300] = {0};
        int ret;
        int numIslands(vector<vector<char>>& grid) {
            int n = grid.size(),m = grid[0].size();
            for(int i = 0 ; i < n ; i++){
                for(int j = 0 ; j < m ; j++){
                    if(grid[i][j] == '1'){
                        is_existent[i][j] = 1;
                        grid[i][j] = '0';
                        dfs(grid,i,j);
                        ret++;
                    }
                }
            }
            return ret;
        }
        int indexx[4] = {0,0,-1,1};
        int indexy[4] = {1,-1,0,0};
        void dfs(vector<vector<char>>& grid,int i, int j){
            int n = grid.size(),m = grid[0].size();
            for(int k = 0 ; k < 4 ; k++){
                int row = indexx[k] + i;
                int col = indexy[k] + j;
                if(row >= 0 && row < n&& col >=0 && col < m && is_existent[row][col] == 0 && grid[row][col] == '1'){
                    is_existent[row][col] = 1;
                    grid[row][col] = '0';
                    dfs(grid,row,col);
                }
            }
            return;
        }
    
    };